I am realigning my fastq reads against contigs generated from metaspades to see which reads mapped to which contig.

After running the following code to get mapped reads:

    samtools view -h -F 4 in.bam > mapped.bam


I am confused by the file output and my next step. Here is a shortened example of the ```mapped.bam``` file (please note I cut off the end of the file just to make things neat as I am only interested in understanding the first few columns):


    A00977:183:HLLKYDSXY:3:1503:16658:5071  73      NODE_5156_length_78_cov_28      1       60      78M27S  =       1       0
    A00977:183:HLLKYDSXY:3:2178:28248:9142  369     NODE_5159_length_78_cov_5       31      0       48M98H  NODE_691_length_1085_cov_115.969        17      0


So the first column is clearly the read name, followed by read length (?), then the reference sequence name. I am not sure what columns 4, 5, 6 represent. I also don't understand why in the 7th column, some have the NODE contig name appear but others do not - what exactly does that mean? 

If my end goal is to determine which contig a read mapped to, I should be looking at the 3rd column or 7th column?
I notice that when I grep a read name, sometimes the read appears multiple times leading me to believe a read has mapped to multiple contigs at a time (fine okay), but which column is giving me the actual contig name that it mapped to? 

I just want to get this type of info to determine read depth for the contigs.
The `samtools view` outputs information from SAM and BAM files in SAM format. You can find a description of the SAM format here: http://samtools.github.io/hts-specs/SAMv1.pdf

Section 1.4 deals with the meaning of each of the mandatory columns. It includes the following table:

```
 Col  Field  Type    Regexp/Range                  Brief description
|---|------|-------|----------------------------|----------------------------------------|
1    QNAME  String  [!-?A-~]{1,254}               Query template NAME
2    FLAG   Int     [0, 216 − 1]                  bitwise FLAG
3    RNAME  String  \*|[:rname:∧*=][:rname:]*     Reference sequence NAME11
4    POS    Int     [0, 231 − 1]                  1-based leftmost mapping POSition
5    MAPQ   Int     [0, 28 − 1]                   MAPping Quality
6    CIGAR  String  \*|([0-9]+[MIDNSHPX=])+       CIGAR string
7    RNEXT  String  \*|=|[:rname:∧*=][:rname:]*   Reference name of the mate/next read
8    PNEXT  Int     [0, 231 − 1]                  Position of the mate/next read
9    TLEN   Int     [−231 + 1, 231 − 1]           observed Template LENgth
10   SEQ    String  \*|[A-Za-z=.]+                segment SEQuence
11   QUAL   String  [!-~]+                        ASCII of Phred-scaled base QUALity+33
```

Column 12 contains a space separated list of *optional* informational tags about the read. 

We can see that the first column is, as you have guessed, the name of the read (or query name). The second column is the FLAG - this is a bitwise flag that encode information about the status of the alignment. Things like is it a successful alignment, is its pair mapped, is it a read1 or a read2?

Finally the third column is the Reference sequence (i.e. the name of the contig the read is aligned to). This is the column you are interested in if you want to know which contig a read is aligned to. And you are correct that reads can align to more than one contig (depending on the configuration of the aligner).

The 7th column, which you note sometimes does and sometimes doesn't contain contig information gives us information about the contig to which the mate of this read aligns. It only contains a contig name if the mate aligns to a different contig. If it aligns to the same contig, this columns will contain `=`. If the mate is unaligned, it will contain `*`. 